package com.camera.yl.widght;
import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.OvershootInterpolator;

import com.camera.base.Log.YLogImpl;

public class RecordingButton extends View {

    private Paint outerCirclePaint;
    private Paint innerCirclePaint;
    private float radius = 0;
    private float innerRadius;
    private boolean isRecording = false;

    private Path innerShapePath;
    private float animatedInnerSize;
    private float animatedCornerRadius;

    private OnRecordingClickListener onRecordingClickListener;

    private boolean delyChange = false;
    public RecordingButton(Context context) {
        super(context);
        init();
    }

    public RecordingButton(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public RecordingButton(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        outerCirclePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        outerCirclePaint.setColor(Color.WHITE);
        outerCirclePaint.setStyle(Paint.Style.STROKE);
        outerCirclePaint.setStrokeWidth(5); // 边框厚度减小

        innerCirclePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        innerCirclePaint.setColor(Color.RED);
        innerCirclePaint.setStyle(Paint.Style.FILL);

        innerShapePath = new Path();
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        radius = Math.min(w, h) / 2f - 10;
        innerRadius = radius * 0.5f; // 中间红色圆的大小减小
        animatedInnerSize = innerRadius * 2f;
        animatedCornerRadius = innerRadius; // 初始状态为圆形
        if (delyChange) {
            delyChange = false;
            toggleRecording();
        }
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        float centerX = getWidth() / 2f;
        float centerY = getHeight() / 2f;

        // Draw outer circle
        canvas.drawCircle(centerX, centerY, radius, outerCirclePaint);

        // Draw inner shape based on animation state
        innerShapePath.reset();
        float halfSize = animatedInnerSize / 2f;
        if (animatedCornerRadius > 0) {
            innerShapePath.addRoundRect(centerX - halfSize, centerY - halfSize, centerX + halfSize, centerY + halfSize,
                    animatedCornerRadius, animatedCornerRadius, Path.Direction.CW);
            canvas.drawPath(innerShapePath, innerCirclePaint);
        } else {
            canvas.drawCircle(centerX, centerY, halfSize, innerCirclePaint);
        }
    }
    public boolean isRecording(){
        return isRecording;
    }
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (event.getAction() == MotionEvent.ACTION_DOWN) {
            toggleRecording();
            if (onRecordingClickListener != null) {
                onRecordingClickListener.onRecordingClick(isRecording);
            }
            return true;
        }
        return super.onTouchEvent(event);
    }

    public void toggleRecording() {
        if (radius != 0){
            isRecording = !isRecording;
            animateInnerShape();
        }else{
            delyChange = true;
        }

    }

    private void animateInnerShape() {
        float startSize = animatedInnerSize;
        float endSize = isRecording ? radius * 1f : innerRadius * 2f;
        float startCornerRadius = animatedCornerRadius;
        float endCornerRadius = isRecording ? innerRadius * 0.3f : innerRadius;

        ValueAnimator sizeAnimator = ValueAnimator.ofFloat(startSize, endSize);
        sizeAnimator.addUpdateListener(animation -> {
            animatedInnerSize = (float) animation.getAnimatedValue();
            invalidate();
        });

        ValueAnimator cornerAnimator = ValueAnimator.ofFloat(startCornerRadius, endCornerRadius);
        cornerAnimator.addUpdateListener(animation -> {
            animatedCornerRadius = (float) animation.getAnimatedValue();
            invalidate();
        });

        sizeAnimator.setInterpolator(new OvershootInterpolator());
        sizeAnimator.setDuration(300);
        sizeAnimator.start();

        cornerAnimator.setInterpolator(new OvershootInterpolator());
        cornerAnimator.setDuration(300);
        cornerAnimator.start();
    }

    // 对外的点击事件接口
    public interface OnRecordingClickListener {
        void onRecordingClick(boolean isRecording);
    }

    public void setOnRecordingClickListener(OnRecordingClickListener listener) {
        this.onRecordingClickListener = listener;
    }
}
